package net.wofly.sale.commodity.validator;


import net.wofly.sale.commodity.domain.Commodity;
import net.wofly.sale.commodity.domain.CommodityChannel;
import net.wofly.sale.commodity.dto.CommodityChannelDto;
import net.wofly.sale.commodity.dto.CommodityDto;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author siqingwei
 */
public class CommodityValidator {

    /**
     * 换行符
     */
    private static String lineSeparator = System.getProperty("line.separator");

    public static String validateAddCommodityParams(CommodityDto commodityDto) {
        StringBuffer stringBuffer = new StringBuffer();

        if (commodityDto == null) {
            stringBuffer.append("商品信息(commodity)不能为空！").append(lineSeparator);
        } else {
            if (StringUtils.isEmpty(commodityDto.getBrandID())) {
                stringBuffer.append("品牌ID(brandID)不能为空！").append(lineSeparator);
            }
            if (StringUtils.isEmpty(commodityDto.getCommodityName())) {
                stringBuffer.append("商品名称(commodityName)不能为空！").append(lineSeparator);
            }
            if (StringUtils.isEmpty(commodityDto.getCommodityTypeID())) {
                stringBuffer.append("商品类型ID(commodityTypeID)不能为空！").append(lineSeparator);
            }

            validateAddCommodityChannelList(stringBuffer, commodityDto.getCommodityChannelDtos());
        }

        return stringBuffer.toString();
    }
    
    public static String validateUpdateCommodityParams(CommodityDto commodityDto) {
        StringBuffer stringBuffer = new StringBuffer();

        if (commodityDto == null) {
            stringBuffer.append("商品信息(commodity)不能为空！").append(lineSeparator);
        } else {
        		if (StringUtils.isEmpty(commodityDto.getCommodityID())) {
                stringBuffer.append("商品ID(commodityID)不能为空！").append(lineSeparator);
            }
            if (StringUtils.isEmpty(commodityDto.getBrandID())) {
                stringBuffer.append("品牌ID(brandID)不能为空！").append(lineSeparator);
            }
            if (StringUtils.isEmpty(commodityDto.getCommodityName())) {
                stringBuffer.append("商品名称(commodityName)不能为空！").append(lineSeparator);
            }
            if (StringUtils.isEmpty(commodityDto.getCommodityTypeID())) {
                stringBuffer.append("商品类型ID(commodityTypeID)不能为空！").append(lineSeparator);
            }
            
            if (null!=commodityDto.getCommodityChannelDtos()) {
            		for(CommodityChannelDto commodityChannelDto:commodityDto.getCommodityChannelDtos()) {
            			if (StringUtils.isEmpty(commodityChannelDto.getCommodityChannelID())) {
                        stringBuffer.append("商品渠道ID(commodityChannelID)不能为空！").append(lineSeparator);
                    }
                }
            }
            
            validateAddCommodityChannelList(stringBuffer, commodityDto.getCommodityChannelDtos());
        }

        return stringBuffer.toString();
    }

    public static String validateAddCommodityChannelDtoParams(CommodityChannelDto commodityChannelDto) {
        StringBuffer stringBuffer = new StringBuffer();

        if (commodityChannelDto == null) {
            stringBuffer.append("商品渠道(commodityChannel)不能为空！").append(lineSeparator);
        } else {
//            if (StringUtils.isEmpty(commodityChannelDto.getCommodityChannelCode())) {
//                stringBuffer.append("商品渠道编码(commodityChannelCode)不能为空！").append(lineSeparator);
//            }
            if (StringUtils.isEmpty(commodityChannelDto.getChannel())) {
                stringBuffer.append("渠道名称(channel)不能为空！").append(lineSeparator);
            }
            if (StringUtils.isEmpty(commodityChannelDto.getCommodityChannelPrise())) {
                stringBuffer.append("商品渠道价(commodityChannelPrise)不能为空！").append(lineSeparator);
            }
        }

        return stringBuffer.toString();
    }

    public static String validateAddCommodityChannelParams(CommodityChannel commodityChannel) {
        StringBuffer stringBuffer = new StringBuffer();

        if (commodityChannel == null) {
            stringBuffer.append("商品渠道(commodityChannel)不能为空！").append(lineSeparator);
        } else {
            if (StringUtils.isEmpty(commodityChannel.getCommodityChannelCode())) {
                stringBuffer.append("商品渠道编码(commodityChannelCode)不能为空！").append(lineSeparator);
            }
            if (StringUtils.isEmpty(commodityChannel.getChannel())) {
                stringBuffer.append("渠道名称(channel)不能为空！").append(lineSeparator);
            }
            if (StringUtils.isEmpty(commodityChannel.getCommodityChannelPrise())) {
                stringBuffer.append("商品渠道价(commodityChannelPrise)不能为空！").append(lineSeparator);
            }
            if (StringUtils.isEmpty(commodityChannel.getCommodityID())) {
                stringBuffer.append("商品ID(commodityID)不能为空！").append(lineSeparator);
            }
        }

        return stringBuffer.toString();
    }

    public static String validateDelCommodityParams(String commodityID) {
        StringBuffer stringBuffer = new StringBuffer();

        if (StringUtils.isEmpty(commodityID)) {
            stringBuffer.append("商品ID(commodityID)不能为空！").append(lineSeparator);
        }

        return stringBuffer.toString();
    }

    public static String validateDelCommodityChannelParams(String commodityChannelID) {
        StringBuffer stringBuffer = new StringBuffer();

        if (StringUtils.isEmpty(commodityChannelID)) {
            stringBuffer.append("商品渠道ID(commodityChannelID)不能为空！").append(lineSeparator);
        }

        return stringBuffer.toString();
    }

    public static String validateGetAllCommodityChannelParams() {
        StringBuffer stringBuffer = new StringBuffer();

        return stringBuffer.toString();
    }

    public static String validateGetCommodityByTypeParams(String commodityTypeID, Integer flag) {
        StringBuffer stringBuffer = new StringBuffer();

        if (StringUtils.isEmpty(commodityTypeID)) {
            stringBuffer.append("商品类型ID(commodityTypeID)不能为空！").append(lineSeparator);
        }

        if (flag == null) {
            stringBuffer.append("标志(flag)不能为空！").append(lineSeparator);
        }

        return stringBuffer.toString();
    }

    public static String validateGetCommodityChannelByIDParams(String commodityID) {
        StringBuffer stringBuffer = new StringBuffer();

        if (StringUtils.isEmpty(commodityID)) {
            stringBuffer.append("商品ID(commodityID)不能为空！").append(lineSeparator);
        }

        return stringBuffer.toString();
    }

    public static String validateUpdateCommodityParams(Commodity commodity) {
        StringBuffer stringBuffer = new StringBuffer();
        if (commodity == null) {
            stringBuffer.append("商品信息(commodity)不能为空！").append(lineSeparator);
        } else {
            if (StringUtils.isEmpty(commodity.getCommodityID())) {
                stringBuffer.append("商品ID(commodityID)不能为空！").append(lineSeparator);
            }
            if (StringUtils.isEmpty(commodity.getBrandID())) {
                stringBuffer.append("品牌ID(brandID)不能为空！").append(lineSeparator);
            }
            if (StringUtils.isEmpty(commodity.getCommodityCode())) {
                stringBuffer.append("商品编码(commodityCode)不能为空！").append(lineSeparator);
            }
            if (StringUtils.isEmpty(commodity.getCommodityName())) {
                stringBuffer.append("商品名称(commodityName)不能为空！").append(lineSeparator);
            }
            if (StringUtils.isEmpty(commodity.getCommodityTypeID())) {
                stringBuffer.append("商品类型ID(commodityTypeID)不能为空！").append(lineSeparator);
            }

            validateUpdateCommodityChannelList(stringBuffer, commodity.getCommodityChannelList());
        }

        return stringBuffer.toString();
    }

    private static void validateAddCommodityChannelList(StringBuffer stringBuffer, List<CommodityChannelDto> commodityChannelDtos){
        if (CollectionUtils.isNotEmpty(commodityChannelDtos)) {
            Set<String> channelCodeSet = new HashSet<>(commodityChannelDtos.size());
        }
    }

    private static void validateUpdateCommodityChannelList(StringBuffer stringBuffer, List<CommodityChannel> commodityChannelList){
        if (CollectionUtils.isNotEmpty(commodityChannelList)) {
            Set<String> channelCodeSet = new HashSet<>(commodityChannelList.size());
            for (CommodityChannel commodityChannel : commodityChannelList) {
                channelCodeSet.add(commodityChannel.getCommodityChannelCode());
                stringBuffer.append(validateUpdateCommodityChannelParams(commodityChannel)).append(lineSeparator);
            }
            if (channelCodeSet.size() != commodityChannelList.size()) {
                stringBuffer.append("商品渠道编码重复！").append(lineSeparator);
            }
        }
    }

    public static String validateUpdateCommodityChannelParams(CommodityChannel commodityChannel) {
        StringBuffer stringBuffer = new StringBuffer();

        if (commodityChannel == null) {
            stringBuffer.append("商品渠道(commodityChannel)不能为空！").append(lineSeparator);
        } else {
            if (StringUtils.isEmpty(commodityChannel.getCommodityChannelID())) {
                stringBuffer.append("商品渠道ID(commodityChannelID)不能为空！").append(lineSeparator);
            }
            if (StringUtils.isEmpty(commodityChannel.getCommodityChannelCode())) {
                stringBuffer.append("商品渠道编码(commodityChannelCode)不能为空！").append(lineSeparator);
            }
            if (StringUtils.isEmpty(commodityChannel.getChannel())) {
                stringBuffer.append("渠道名称(channel)不能为空！").append(lineSeparator);
            }
            if (StringUtils.isEmpty(commodityChannel.getCommodityChannelPrise())) {
                stringBuffer.append("商品渠道价(commodityChannelPrise)不能为空！").append(lineSeparator);
            }
            if (StringUtils.isEmpty(commodityChannel.getCommodityID())) {
                stringBuffer.append("商品ID(commodityID)不能为空！").append(lineSeparator);
            }
        }

        return stringBuffer.toString();
    }
}
